# ------------------------------------------------------------------------------
# Creates yield/costeff/test rate table
# Updates author: Cassidy Shubatt <cshubatt@gmail.com>
# To run: bash 03_yield_costeff_table.sh
# ------------------------------------------------------------------------------

# Libraries --------------------------------------------------------------------
library(here)
library(yaml)
library(data.table)
library(scales)
library(tidyverse)
library(glue) # glue
library(xtable)

u <- modules::use(here::here("lib", "util.R"))
temp <- here("code", "04_cost_effectiveness", "temp")

# Load Data --------------------------------------------------------------------
message("Loading data...")
paths <- read_yaml(here("lib", "filepaths.yml"))
overnight_lab <- ""
daly_cost <- readRDS(paths$analysis$daly_cost) %>%
  select(ed_enc_id, cost, daly_100, daly_200, daly_250, daly_300, daly_500)
cohort <- readRDS(glue(paths$analysis$test_cohort)) %>%
  u$safe_left_join(daly_cost) %>%
  filter(!exclude) %>%
  mutate(Quintile = tile_stent_or_cabg_005_tested) %>%
  mutate(Decile = tile_stent_or_cabg_010_tested)


# Get Mean Table ---------------------------------------------------------------
message("Getting mean table...")
costeff <- cohort %>%
  filter(test_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    rate = (sum(cost, na.rm = TRUE) / sum(daly_250, na.rm = TRUE)) %>% trunc(),
    costeff_low = (sum(cost, na.rm = TRUE) / sum(daly_300, na.rm = TRUE)) %>% trunc(),
    costeff_hi = (sum(cost, na.rm = TRUE) / sum(daly_200, na.rm = TRUE)) %>% trunc()
  ) %>%
  ungroup() %>%
  mutate(se = glue("({costeff_low}-{costeff_hi})")) %>%
  select(-c(costeff_low, costeff_hi)) %>%
  gather(key = "Term", value = "costeff", -Quintile)

yield_tested <- cohort %>%
  filter(test_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(stent_or_cabg_010_day) %>% round(3),
    std.deviation = sd(stent_or_cabg_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "yield", -Quintile)

test_rates <- cohort %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(test_010_day) %>% round(3),
    std.deviation = sd(test_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "test", -Quintile)

comb <- yield_tested %>%
  u$safe_left_join(costeff) %>%
  u$safe_left_join(test_rates) %>%
  .[order(.[["Quintile"]]), ] %>%
  mutate(
    Quintile = ifelse(
      Term == "se", "", Quintile
    )
  ) %>%
  mutate(
    yield = ifelse(
      Term == "se", glue("({yield})"), yield
    )
  ) %>%
  mutate(
    test = ifelse(
      Term == "se", glue("({test})"), test
    )
  ) %>%
  select(-Term)

# Latex Table
message("Making latex table...")
xt <- xtable(comb)

# Save -------------------------------------------------------------------------
message("Saving main table...")
print(
  xt,
  type = "latex", file = file.path(temp, "yield_costeff_tbl.tex"),
  include.rownames = FALSE
)

# Cath Table (First Test)-------------------------------------------------------
message("Making cath table (for first test == cath)...")
costeff_cath <- cohort %>%
  filter(first_test == "cath" & cath_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    rate = (sum(cost, na.rm = TRUE) / sum(daly_250, na.rm = TRUE)) %>% trunc(),
    costeff_low = (sum(cost, na.rm = TRUE) / sum(daly_300, na.rm = TRUE)) %>% trunc(),
    costeff_hi = (sum(cost, na.rm = TRUE) / sum(daly_200, na.rm = TRUE)) %>% trunc()
  ) %>%
  ungroup() %>%
  mutate(se = glue("({costeff_low}-{costeff_hi})")) %>%
  select(-c(costeff_low, costeff_hi)) %>%
  gather(key = "Term", value = "costeff_cath", -Quintile)

yield_tested <- cohort %>%
  filter(first_test == "cath" & cath_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(stent_or_cabg_010_day) %>% round(3),
    std.deviation = sd(stent_or_cabg_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "yield_cath", -Quintile)

test_rates <- cohort %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(first_test == "cath" & cath_010_day) %>% round(3),
    std.deviation = sd(first_test == "cath" & cath_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "cath", -Quintile)

comb <- yield_tested %>%
  u$safe_left_join(test_rates) %>%
  u$safe_left_join(costeff_cath) %>%
  .[order(.[["Quintile"]]), ] %>%
  mutate(
    Quintile = ifelse(
      Term == "se", "", Quintile
    )
  ) %>%
  mutate(
    yield_cath = ifelse(
      Term == "se", glue("({yield_cath})"), yield_cath
    )
  ) %>%
  mutate(
    cath = ifelse(
      Term == "se", glue("({cath})"), cath
    )
  ) %>%
  select(-Term)

# Latex Table
message("Making latex table for cath...")
xt <- xtable(comb)

# Save -------------------------------------------------------------------------
message("Saving cath table...")
print(
  xt,
  type = "latex", file = file.path(temp, "yield_costeff_tbl_cath_first.tex"),
  include.rownames = FALSE
)

# Cath Table (All) -------------------------------------------------------------
message("Making cath table (for all cath)...")
costeff_cath <- cohort %>%
  filter(cath_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    rate = (sum(cost, na.rm = TRUE) / sum(daly_250, na.rm = TRUE)) %>% trunc(),
    costeff_low = (sum(cost, na.rm = TRUE) / sum(daly_300, na.rm = TRUE)) %>% trunc(),
    costeff_hi = (sum(cost, na.rm = TRUE) / sum(daly_200, na.rm = TRUE)) %>% trunc()
  ) %>%
  ungroup() %>%
  mutate(se = glue("({costeff_low}-{costeff_hi})")) %>%
  select(-c(costeff_low, costeff_hi)) %>%
  gather(key = "Term", value = "costeff_cath", -Quintile)

yield_tested <- cohort %>%
  filter(cath_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(stent_or_cabg_010_day) %>% round(3),
    std.deviation = sd(stent_or_cabg_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "yield_cath", -Quintile)

test_rates <- cohort %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(cath_010_day) %>% round(3),
    std.deviation = sd(cath_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "cath", -Quintile)

comb <- yield_tested %>%
  u$safe_left_join(test_rates) %>%
  u$safe_left_join(costeff_cath) %>%
  .[order(.[["Quintile"]]), ] %>%
  mutate(
    Quintile = ifelse(
      Term == "se", "", Quintile
    )
  ) %>%
  mutate(
    yield_cath = ifelse(
      Term == "se", glue("({yield_cath})"), yield_cath
    )
  ) %>%
  mutate(
    cath = ifelse(
      Term == "se", glue("({cath})"), cath
    )
  ) %>%
  select(-Term)

# Latex Table
message("Making latex table for cath...")
xt <- xtable(comb)

# Save -------------------------------------------------------------------------
message("Saving cath table...")
print(
  xt,
  type = "latex", file = file.path(temp, "yield_costeff_tbl_cath_all.tex"),
  include.rownames = FALSE
)

# Stress Table -------------------------------------------------------------------
message("Making stress table...")
costeff_stress <- cohort %>%
  filter(first_test == "stress") %>%
  group_by(Quintile) %>%
  summarize(
    rate = (sum(cost, na.rm = TRUE) / sum(daly_250, na.rm = TRUE)) %>% trunc(),
    costeff_low = (sum(cost, na.rm = TRUE) / sum(daly_300, na.rm = TRUE)) %>% trunc(),
    costeff_hi = (sum(cost, na.rm = TRUE) / sum(daly_200, na.rm = TRUE)) %>% trunc()
  ) %>%
  ungroup() %>%
  mutate(se = glue("({costeff_low}-{costeff_hi})")) %>%
  select(-c(costeff_low, costeff_hi)) %>%
  gather(key = "Term", value = "costeff_stress", -Quintile)

yield_tested <- cohort %>%
  filter(stress_010_day) %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(stent_or_cabg_010_day) %>% round(3),
    std.deviation = sd(stent_or_cabg_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "yield_stress", -Quintile)

test_rates <- cohort %>%
  group_by(Quintile) %>%
  summarize(
    n = n(),
    rate = mean(stress_010_day) %>% round(3),
    std.deviation = sd(stress_010_day),
    se = (std.deviation / sqrt(n)) %>% round(3)
  ) %>%
  select(-n, -std.deviation) %>%
  gather(key = "Term", value = "stress", -Quintile)

comb <- yield_tested %>%
  u$safe_left_join(test_rates) %>%
  u$safe_left_join(costeff_stress) %>%
  .[order(.[["Quintile"]]), ] %>%
  mutate(
    Quintile = ifelse(
      Term == "se", "", Quintile
    )
  ) %>%
  mutate(
    yield_stress = ifelse(
      Term == "se", glue("({yield_stress})"), yield_stress
    )
  ) %>%
  mutate(
    stress = ifelse(
      Term == "se", glue("({stress})"), stress
    )
  ) %>%
  select(-Term)

# Latex Table
message("Making latex table for cath...")
xt <- xtable(comb)

# Save -------------------------------------------------------------------------
message("Saving cath table...")
print(
  xt,
  type = "latex", file = file.path(temp, "yield_costeff_tbl_stress_first.tex"),
  include.rownames = FALSE
)

message("Done.")
